home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / serverlib / sync / waitLatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  3.5 KB  |  147 lines

  1. /*
  2.  *   $RCSfile: waitLatch.c,v $  
  3.  *   $Revision: 1.1.1.1 $  
  4.  *   $Date: 1996/05/04 21:55:59 $      
  5.  */ 
  6. /**********************************************************************
  7. * EXODUS Database Toolkit Software
  8. * Copyright (c) 1991 Computer Sciences Department, University of
  9. *                    Wisconsin -- Madison
  10. * All Rights Reserved.
  11. *
  12. * Permission to use, copy, modify and distribute this software and its
  13. * documentation is hereby granted, provided that both the copyright
  14. * notice and this permission notice appear in all copies of the
  15. * software, derivative works or modified versions, and any portions
  16. * thereof, and that both notices appear in supporting documentation.
  17. *
  18. * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  19. * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  20. * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  22. *
  23. * The EXODUS Project Group requests users of this software to return 
  24. * any improvements or extensions that they make to:
  25. *
  26. *   EXODUS Project Group 
  27. *     c/o David J. DeWitt and Michael J. Carey
  28. *   Computer Sciences Department
  29. *   University of Wisconsin -- Madison
  30. *   Madison, WI 53706
  31. *
  32. *     or exodus@cs.wisc.edu
  33. *
  34. * In addition, the EXODUS Project Group requests that users grant the 
  35. * Computer Sciences Department rights to redistribute these changes.
  36. **********************************************************************/
  37.  
  38. #include "sysdefs.h"
  39. #include "ess.h"
  40. #include "checking.h"
  41. #include "trace.h"
  42. #include "error.h"
  43. #include "list.h"
  44. #include "tid.h"
  45. #include "io.h"
  46. #include "lock.h"
  47. #include "object.h"
  48. #include "msgdefs.h"
  49. #include "thread.h"
  50. #include "latch.h"
  51. #include "threadstate.h"
  52. #include "thread_globals.h"
  53.  
  54.  
  55.  int
  56. waitLatch (
  57.  
  58.     register LATCH    *latch,
  59.     int                latchType 
  60. )
  61. {
  62.  
  63.     TRACE(TR_LATCH, TR_LEVEL_1);
  64.  
  65.     /*
  66.      *    check the semaphore magic number
  67.      */
  68.     CHECK_LATCH_MAGIC(latch);
  69.  
  70.     /*
  71.      *    check to see what type of latch is required
  72.      */
  73.     switch (latchType)    {
  74.  
  75.         case SHARE_LATCH:
  76.  
  77.             /*
  78.              *    check to see if there is an exclusive
  79.              *    latch held.  If not, check to see if there
  80.              *    are waiters which must be exclusive
  81.              */
  82.             if ((latch->exclusiveFlag == 0) && (LIST_EMPTY( &(latch->waitList) )))    {
  83.  
  84.                 /*
  85.                  *    just bump the share count
  86.                  */
  87.                 latch->shareCount++;
  88.                 TRPRINT(TR_LATCH, TR_LEVEL_2, ("%d share holders", latch->shareCount));
  89.                 return(esmNOERROR);
  90.  
  91.             } else {
  92.  
  93.                 /*
  94.                  *    queue the request
  95.                  */
  96.                 TRPRINT(TR_LATCH, TR_LEVEL_2, ("held exclusive"));
  97.                 Active->error = esmNOERROR;
  98.                 listEnq( &(latch->waitList), &(Active->controlList) );
  99.                 dispatch(THREAD_SH_LATCH_WAIT);
  100.  
  101.                 /*
  102.                  *    return error code
  103.                  */
  104.                 return(Active->error);
  105.             }
  106.             break;
  107.  
  108.         case EXCLUSIVE_LATCH:
  109.  
  110.             /*
  111.              *    check to see if there is an exclusive latch held
  112.              */
  113.             if ((latch->exclusiveFlag == 0) && (latch->shareCount == 0))    {
  114.  
  115.                 /*
  116.                  *    get the exclusive latch
  117.                  */
  118.                 TRPRINT(TR_LATCH, TR_LEVEL_2, ("latch free"));
  119.                 latch->exclusiveFlag = 1;
  120.                 return(esmNOERROR);
  121.             
  122.             } else {
  123.  
  124.                 /*
  125.                  *    put on wait list
  126.                  */
  127.                 TRPRINT(TR_LATCH, TR_LEVEL_2, ("exclusive:%d share%d holders",
  128.                         latch->exclusiveFlag, latch->shareCount));
  129.                 Active->error = esmNOERROR;
  130.                 listEnq( &(latch->waitList), &(Active->controlList) );
  131.                 dispatch(THREAD_EX_LATCH_WAIT);
  132.  
  133.                 /*
  134.                  *    return error code
  135.                  */
  136.                 return(Active->error);
  137.             }
  138.             break;
  139.  
  140.         default:
  141.  
  142.             SM_ERROR(TYPE_FATAL, esmINTERNAL);
  143.             break;
  144.     }
  145.     return esmFAILURE; /* to keep the compiler quiet */
  146. }
  147.